﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using UCL.Powersource;

namespace System.Collections.Specialized
{

    public static class FormattingExtensions
    {
        public static string GetNotifyCollectionChangedEventArgsString(this NotifyCollectionChangedEventArgs e)
        {
            if (e == null) return "#NULL#";
            else
            {
                var newItemstr = "#NULL";
                var oldItemstr = "#NULL";
                if (e.NewItems != null)
                {
                    var newItemStrList = new List<String>();
                    foreach (var it in e.NewItems)
                    {
                        if (it != null) newItemStrList.Add(it.ToString()); else newItemStrList.Add("#NULL");
                    }
                    newItemstr = string.Join(", ", newItemStrList);

                }
                if (e.OldItems != null)
                {
                    var oldItemStrList = new List<String>();
                    foreach (var it in e.OldItems)
                    {
                        if (it != null) oldItemStrList.Add(it.ToString()); else oldItemStrList.Add("#NULL");
                    }
                    oldItemstr = string.Join(", ", oldItemStrList);

                }
                return string.Format("Action {0}, NewItems : {1}, OldItems : {2}", e.Action, newItemstr, oldItemstr);
            }
        }

    }

}
namespace UCL.Powersource.Helpers
{
    public static class PropertyExtensions
    {
        public static string GetPropertyName<TProperty>(Expression<Func<TProperty>> property)
        {
            return property.GetMemberInfo().Name;
        }

        // Unable to infer the type Func<TProperty> is better usage
        //public static string GetPropertyName2<TProperty>(Expression<TProperty> property)
        //{
        //    return property.GetMemberInfo().Name;
        //}

        public static bool IsPropertyChanged<TProperty>(PropertyChangedEventArgs e, Expression<Func<TProperty>> property)
        {
            var propName = GetPropertyName(property);
            return propName == e.PropertyName;

        }


    }
}
